﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    class AnimatedObject : Object2D
    {
        // Animation[] array = new Animation[10]();
        List<Animation> animations;
        Animation currentAnimation;

        public AnimatedObject(List<Animation> animations,string startAnimationName, Texture2D image, bool lockImage)
            : base(image, lockImage)
        {
            this.animations = animations;
            currentAnimation = GetAnimation(startAnimationName);
        }




        public Animation MyAnimation
        {
            get
            {
                return currentAnimation;
            }
            set
            {
                if (currentAnimation.Nome != value.Nome)
                {
                    currentAnimation = value;
                }
            }
        }





        public Animation GetAnimation(string name)
        {
            return animations.Find(delegate(Animation a) { return a.Nome == name; });
        }


        public override void Update(GameTime gt)
        {
            if (currentAnimation.currentInterval + currentAnimation.duration < (float)gt.TotalGameTime.TotalMilliseconds)
            {
                return;
            }
            currentAnimation.currentInterval = (float)gt.TotalGameTime.TotalMilliseconds;
            currentAnimation.currentFrame.X++;
            if (currentAnimation.currentFrame.X >= currentAnimation.totalFrames.X)
            {
                currentAnimation.currentFrame.X = 0;
                currentAnimation.currentFrame.Y++;
                if (currentAnimation.currentFrame.Y >= currentAnimation.totalFrames.Y)
                {
                    currentAnimation.currentFrame.Y = 0;
                }
            }
            Rect = new Rectangle(
                (int)(currentAnimation.startPosition.X + currentAnimation.frameSize.X * currentAnimation.currentFrame.X),
                (int)(currentAnimation.startPosition.Y + currentAnimation.frameSize.Y * currentAnimation.currentFrame.Y),
                (int)currentAnimation.frameSize.X, (int)currentAnimation.frameSize.Y);
        }

    }

    /*
     * Animacao: Nome-
     * Tempo(FPS): duracao, tempoAtual
     * Frames: quantidade X e Y, tamanho,
     */

    struct Animation
    {
        public string Nome;

        public float duration;
        public float currentInterval;

        public Vector2 currentFrame;
        public Vector2 totalFrames;
        public Vector2 frameSize;

        public Vector2 startPosition;

        /// <summary>
        /// Animação a ser usada com a classe AnimatedObject
        /// </summary>
        /// <param name="n">Nome da animação</param>
        /// <param name="d">Quanto tempo que a animação dura
        /// <example>200 milisegundos ou 1000/24 (24fps)</example>
        /// </param>
        /// <param name="t">Quantidade de frames
        /// <example>Animação de 8 frames horizontalmente e contem 2 fileiras: Vector2(8x2)</example>
        /// </param>
        /// <param name="f">Temanho de frame
        /// <example>TODOS os frames da animação são 20x20</example>
        /// </param>
        /// <param name="s">Posição inicial da animação em proporção da imagem
        /// <example>Imagem e 200x200 dentro a imagem a animação começa no pixel 0x20</example>
        /// </param>
        public Animation(string n, float d, Vector2 t, Vector2 f, Vector2 s)
        {
            Nome = n;
            duration = d;
            currentInterval = 0;
            currentFrame = Vector2.Zero;
            totalFrames = t;
            frameSize = f;
            startPosition = s;
        }

        /// <summary>
        /// Faz um clone do objeto passado como parametro
        /// </summary>
        /// <param name="a">Objeto a ser clonado</param>
        public Animation(Animation a)
        {
            Nome = a.Nome;
            duration = a.duration;
            currentInterval = a.currentInterval;
            currentFrame = a.currentFrame;
            totalFrames = a.totalFrames;
            frameSize = a.frameSize;
            startPosition = a.startPosition;
        }
    }
}